/**
 * 
 */
package com.loyom.data;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * @author xukai
 * 
 */
public class BaseDao {

    private final Logger log = Logger.getLogger(BaseDao.class);

    private final String url, userName, password, className;

    private Connection con = null;

    private static BaseDao dbConnection = null;

    private static final String FILE_NAME = "/databse.properties";

    public BaseDao() {

        Properties pro = new Properties();
        try {
            String path = this.getClass().getResource("/").getPath() + FILE_NAME;
            pro.load(new FileInputStream(path));
            log.info("Properties load successfully!");
        } catch (FileNotFoundException e) {
            log.error(e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        url = pro.getProperty("url");
        userName = pro.getProperty("username");
        password = pro.getProperty("password");
        className = "com.mysql.jdbc.Driver";
    }

    /**
     * 通过单例模式得到一个数据库连接类对象
     * 
     * @return
     * @throws EmlException
     */
    public synchronized static BaseDao getDBConnection() {

        if (dbConnection == null) {
            dbConnection = new BaseDao();
        }
        return dbConnection;
    }

    /**
     * 得到一个数据库链接
     * 
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public Connection getConnection() {

        try {
            Class.forName(className);
            this.con = DriverManager.getConnection(url, userName, password);
            log.info("get one SQL Connection");
        } catch (SQLException e) {
            log.error(e.getMessage());
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return con;
    }

    /**
     * 关闭数据库链接
     * 
     * @param rs
     * @param pre
     * @param con
     * @throws SQLException
     */
    public void closeConnection(ResultSet rs, PreparedStatement pre, Connection con) {

        try {
            if (rs != null) {
                rs.close();
            }
            if (pre != null) {
                pre.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }
    }

    public boolean update(String sql) {

        try {

            BaseDao db = BaseDao.getDBConnection();
            Connection con = db.getConnection();
            con.setAutoCommit(false);
            PreparedStatement ps = con.prepareStatement(sql);
            int result = ps.executeUpdate();
            con.commit();
            return result > 0 ? true : false;
        } catch (SQLException e) {

        }
        return false;
    }

    public Object getSclare(String sql) {

        Object obj = null;

        try {

            BaseDao db = BaseDao.getDBConnection();
            Connection con = db.getConnection();
            con.setAutoCommit(false);
            Statement ps = con.createStatement();
            ResultSet set = ps.executeQuery(sql);
            if (set.next()) {
                obj = set.getObject(1);
            }
            con.commit();
        } catch (SQLException e) {
            log.info("", e);
        }
        return obj;
    }
}
